package model

// Tag
type Tag struct {
	ID    uint   `gorm:"primary_key;not null;auto_increment" json:"id"`
	Title string `gorm:"type:varchar(32);unique;not null" json:"title" binding:"required"`

	Articles []*Article `gorm:"many2many:article_tags"`
}

func GetTags() (insts []Tag, err error) {
	err = DB.Find(&insts).Error

	return
}

func (inst *Tag) Add() (err error) {
	err = DB.Create(&inst).Error
	return
}

func DeleteTag(id uint) (err error) {
	err = DB.Delete(&Tag{}, id).Error
	if err == nil {
		DB.Exec("delete from article_tags where tag_id=?", id)
	}
	return
}
func GetTagArticles(id uint) (inst Tag, err error) {
	err = DB.Preload("Articles").Where("id=?", id).First(&inst).Error
	return
}
